///////////////////////////////////////////////////
// Election Administration Polarization
// Term Limits Table
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"

* Bring in the presidential election analysis data
use "$path/analysis_data/did_election_analysis_data.dta", clear
keep if office=="pres"
keep if state=="IN"
keep if inc==1

* Create a county id
egen county_id = group(state county)

* Compute the change in Democratic presidential vote share
gen diff = vs_dem - lag_vs_dem4

* Create a matrix to store regression output
matrix define B = J(4, 3, .)

* Regression: change in vote share when term limited, Dem vs Rep clerks
reg diff dem, vce(clust county_id)
matrix B[1,1] = _b[dem]
matrix B[2,1] = _se[dem]
matrix B[3,1] = e(N_clust)
matrix B[4,1] = e(N)

* Regression: change in vote share when term limited, Dem vs Rep clerks,
*   adjusting for election year
reg diff dem i.policy_year, vce(clust county_id)
matrix B[1,2] = _b[dem]
matrix B[2,2] = _se[dem]
matrix B[3,2] = e(N_clust)
matrix B[4,2] = e(N)

* Regression: change in vote share when term limited, Dem vs Rep clerks,
*   adjusting for election year and general polarization
reg diff dem i.policy_year lag_vs_dem4, vce(clust county_id)
matrix B[1,3] = _b[dem]
matrix B[2,3] = _se[dem]
matrix B[3,3] = e(N_clust)
matrix B[4,3] = e(N)

* Build the term limits table
qui {
cap log close
set linesize 255
log using "$path/output/table_term_limit.tex", text replace
noi di "\begin{table}[t]"
noi di "\centering"
noi di "\caption{\textbf{Estimates of Increase in Partisan Advantage Provided by Term-Limited Clerks.}"
noi di "\label{tab:table_term_limit}}"
noi di "\begin{tabular}{lccc}"
noi di "\toprule \toprule"
noi di " & \multicolumn{3}{c}{Change in Dem}\\"
noi di " & \multicolumn{3}{c}{Pres Vote Share}\\"
noi di " & (1) & (2) & (3) \\"
noi di "\midrule"
noi di "Dem Elec Official & " %4.3f B[1,1] " & " %4.3f B[1,2] " & " %4.3f B[1,3] "\\"
noi di " & (" %4.3f B[2,1] ") & (" %4.3f B[2,2] ") & (" %4.3f B[2,3] ") \\[2mm]"
noi di "\midrule"
noi di "Counties & " %4.0f B[3,1] " & " %4.0f B[3,2] " & " %4.0f B[3,3] " \\ "
noi di "N & " %4.0f B[4,1] " & " %4.0f B[4,2] " & " %4.0f B[4,3] " \\ "
noi di "Year FE & No & Yes & Yes \\ "
noi di "Lag Dem Vote Share & No & No & Yes \\ "
noi di "\bottomrule \bottomrule"
noi di "\multicolumn{4}{p{0.48\textwidth}}{\footnotesize Robust standard errors clustered by county in parentheses. "
noi di "The data is limited to term-limited, incumbent clerks in Indiana. The outcome is the change in Democratic "
noi di "presidential vote share from the first term to the second term of the term-limited clerk.}"
noi di "\end{tabular}"
noi di "\end{table}"
log off
}

